If you give a width of 100px or 100%, the height will be definitely 100px or 100% because the browser calculated based on the aspect ratio. You can make your image fit on the screen by using the cover value in the CSS background-size property. It also fits the portrait image, but the image looks bigger. So you use the landscape image.
How to set background image size in CSS?
You must enter your image format next to the image name. CSS Background image size is the size of your image and it is used to specify background image size that is width and height of the background image. We can control the width and height of the background image by using CSS background-size property.
How does background-image CSS work?
The background-image CSS property sets one or more background images on an element. The background images are drawn on stacking context layers on top of each other. The first layer specified is drawn as if it is closest to the user. The borders of the element are then drawn on top of them, and the background-color is drawn beneath them.
How to set a background size to cover a Div?
Setting the background-size to cover will cover the entire width of container. It will stretch the image within the width of container. We can set image to fit within the container using image width:100% and height:100%. Use the following code: If we want to fit the image within the div we will use following CSS and html code: